﻿@model Lead2012.ViewModels.LeadViewModel
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Thêm khách hàng";

    // Local variables
    int i = 0;
    string inputTagName = "id";
}

<form id="myForm" action="/Lead/Add" method="post" class="form" enctype="multipart/form-data">
<fieldset>

    <div class="content">
        <h2 class="app">
            <strong>Thêm khách hàng</strong>
        </h2>

        <div class="title nomargin">
            <span class="right-btns">
                <a href="/Lead" class="btn btn-cancel-small">cancel</a>
                <span class="v-line"></span>
                <input type="submit" id="topSubmit" class="btn btn-submit-small" value="Submit" style="vertical-align: top;" onclick="return preValidateLeadForm(this);" />
            </span>
            <h3>Dữ liệu khách hàng</h3>
        </div>

        <!-- header form -->
        <span id="toolbargroup" class="toolbar" data-bind="attr: {'class': toolbarClass}">
            <span class="area full">
                <div class="spacer15" data-bind="visible: EnableMessage"></div>
                <div class="error-msg-orange" data-bind="visible: EnableMessage">
                    <span class="errors" data-bind="text: PleaseMessage"></span>
                </div>
                <span data-bind="visible: EnableLeadType">
                    <strong>Loại:</strong>
                    <div id="divType" class="select-holder sel-lead-type" style="width:175px">
                        <select id="LeadType" class="select" data-bind="value: LeadType, foreach: LeadTypeOptions">
                            <option data-bind="value: ID, text: ItemName"></option>
                        </select>
                        <input type="hidden" id="leadTypeSelected" name="LeadType" value="@Model.LeadTypeId" />
                    </div>
                </span>
                    <strong>Nguồn:</strong>
                <div id="divSource" class="select-holder sel-lead-source" style="width:210px">
                    <select id="LeadSource" name="LeadSource" class="select" data-bind="value: LeadSource, foreach: LeadSourceOptions">
                        <option data-bind="value: ID, text: ItemName"></option>
                    </select>
                    <input type="hidden" id="leadSourceSelected" value="@Model.LeadSourceId" />
                </div>
                <span data-bind="visible: (UserOptions() != null && UserOptions().length > 0)">
                    <strong>Người sở hữu:</strong>
                    <div id="divOwner"class="select-holder sel-lead-user" style="width:210px">
                        <select id="select3" class="select" data-bind="value: User, foreach: UserOptions" name="User">
                            <option data-bind="value: ID, text: ItemName"></option>
                        </select>
                    </div>
                </span>
            </span>
        </span>

        <!-- Error messages -->
        <div id="groupform">
            <div id="errorMessages" style="display:none;">
                <span class="errorgrid"></span>
            </div>
            <div class="clear"></div>

            <!-- Group tabs -->
            <span id="grouptab" class="toolbar grouptab">
                @if (Model != null || Model.ListTab != null)
                {
                    foreach (var tab in Model.ListTab)
                    {
                        <span class="area button">
                            <a class="nexttab" href="#tab@(tab.Id)">@tab.Title<span class="arr"></span> </a>
                        </span>
                    }
                }
            </span>
            <div class="clear"></div>

            <!-- Group Fields -->
            <div id="groupfields" class="panel grouped-form">
                @if (Model == null || Model.ListTab == null)
                {
                    Html.RenderPartial("_PartialFirstEnterLead");
                }
                else
                {
                    foreach (var group in Model.ListTab)
                    {
                        <ul id="tab@(group.Id)" class="accordeon-tabs accordeon-2">
                        @if (group != null && group.Fields != null)
                        {
                            <li class="open">
                                <span class="panel">
                                    @foreach (var itemField in group.Fields)
                                    {
                                        if (itemField.TypeId == 32 || itemField.TypeId == 64)
                                        {
                                            // Dropdown
                                            <div class="row">
                                                <label for="text1">@itemField.Title</label>
                                                <div class="select-holder">
                                                @if (!string.IsNullOrEmpty(itemField.Visibility) && itemField.Visibility.ToUpper() == "READONLY")
                                                {
                                                    <select name="@itemField.Id" class="select readonly" disabled="disabled">
                                                        @foreach (var option in itemField.DataSource)
                                                        {
                                                            <option value="@option.Value">@option.Text</option>
                                                        }
                                                    </select>
                                                }
                                                else
                                                {
                                                    if (itemField.Required)
                                                    {
                                                        <select name="@itemField.Id" class="select" validation="ddlrequired">
                                                            @foreach (var option in itemField.DataSource)
                                                            {
                                                                <option value="@option.Value">@option.Text</option>
                                                            }
                                                        </select>
                                                    }
                                                    else
                                                    {
                                                        <select name="@itemField.Id" class="select">
                                                            @foreach (var option in itemField.DataSource)
                                                            {
                                                                <option value="@option.Value">@option.Text</option>
                                                            }
                                                        </select>
                                                    }
                                                }
                                                </div>
                                                @if (!string.IsNullOrEmpty(itemField.ToolTip))
                                                {
                                                    <span title="@itemField.ToolTip" class="info" href="#"><em>info</em></span>
                                                }
                                            </div>
                                        }
                                        else if (itemField.TypeId == 512)
                                        {
                                            // Checkbox
                                            <div class="row">
                                                <label for="text1">@itemField.Title</label>
                                                <div class="groupinput">
                                                @if (!string.IsNullOrEmpty(itemField.Visibility) && itemField.Visibility.ToUpper() == "READONLY")
                                                {
                                                    <input type="checkbox" name="@itemField.Id" disabled="disabled" />
                                                }
                                                else
                                                {
                                                    <input type="checkbox" name="@itemField.Id" />
                                                }

                                                </div>
                                                @if (!string.IsNullOrEmpty(itemField.ToolTip))
                                                {
                                                    <span title="@itemField.ToolTip" class="info" href="#"><em>info</em></span>
                                                }
                                            </div>
                                        }
                                        else if (itemField.TypeId == 1024)
                                        {
                                            // Radio button list
                                            <div class="row">
                                                <label for="text1">@itemField.Title</label>
                                                <div class="groupinput">
                                                    @if (itemField.DataSource != null)
                                                    {
                                                        if (!string.IsNullOrEmpty(itemField.Visibility) && itemField.Visibility.ToUpper() == "READONLY")
                                                        {
                                                            if (itemField.Required)
                                                            {
                                                                i = 0;
                                                                foreach (var temp in itemField.DataSource)
                                                                {
                                                                    i++;
                                                                    inputTagName = "id" + i.ToString();

                                                                    if (temp.Selected)
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" disabled="disabled" validation="rdrequired" value="@temp.Value" checked="checked" /> @temp.Text
                                                                    }
                                                                    else
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" disabled="disabled" validation="rdrequired" value="@temp.Value" /> @temp.Text
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                i = 0;
                                                                foreach (var temp in itemField.DataSource)
                                                                {
                                                                    i++;
                                                                    inputTagName = "id" + i.ToString();

                                                                    if (temp.Selected)
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" disabled="disabled" value="@temp.Value" checked="checked" /> @temp.Text
                                                                    }
                                                                    else
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" disabled="disabled" value="@temp.Value" /> @temp.Text
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (itemField.Required)
                                                            {
                                                                i = 0;
                                                                foreach (var temp in itemField.DataSource)
                                                                {
                                                                    i++;
                                                                    inputTagName = "id" + i.ToString();

                                                                    if (i == 1)
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" validation="rdrequired" value="@temp.Value" /> @temp.Text
                                                                    }
                                                                    else
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" value="@temp.Value" /> @temp.Text
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                i = 0;
                                                                foreach (var temp in itemField.DataSource)
                                                                {
                                                                    i++;
                                                                    inputTagName = "id" + i.ToString();

                                                                    if (temp.Selected)
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" value="@temp.Value" checked="checked" /> @temp.Text
                                                                    }
                                                                    else
                                                                    {
                                                                        <input id="@inputTagName" type="radio" name="@itemField.Id" value="@temp.Value" /> @temp.Text
                                                                    }
                                                                }
                                                            }
                                                        }

                                                    }
                                                </div>
                                                @if (!string.IsNullOrEmpty(itemField.ToolTip))
                                                {
                                                    <span title="@itemField.ToolTip" class="info" href="#"><em>info</em></span>
                                                }
                                            </div>
                                        }
                                        else
                                        {
                                            if (itemField.Required)
                                            {
                                                Html.RenderPartial("_PartialGroupRequireFields", itemField);
                                            }
                                            else
                                            {
                                                Html.RenderPartial("_PartialGroupFields", itemField);
                                            }
                                        }
                                    }
                                </span>
                            </li>
                        }
                        @if (group != null && group.Groups != null)
                        {
                            foreach (var itemGroup in group.Groups)
                            {
                                if (itemGroup.Fields != null)
                                {
                                    <li class="open">
                                        <span class="tab">
                                            <span>@itemGroup.Title</span>
                                            <span class="expander"></span>
                                        </span>
                                        <span class="panel">
                                            @foreach (var subField in itemGroup.Fields)
                                            {
                                                if (subField.TypeId == 32 || subField.TypeId == 64)
                                                {
                                                    // Dropdown
                                                    <div class="row">
                                                        <label for="text1">@subField.Title</label>
                                                        <div class="select-holder">
                                                        @if (!string.IsNullOrEmpty(subField.Visibility) && subField.Visibility.ToUpper() == "READONLY")
                                                        {
                                                            <select name="@subField.Id" class="select readonly" disabled="disabled">
                                                                @foreach (var option in subField.DataSource)
                                                                {
                                                                    <option value="@option.Value">@option.Text</option>
                                                                }
                                                            </select>
                                                        }
                                                        else
                                                        {
                                                            if (subField.Required)
                                                            {
                                                                <select name="@subField.Id" class="select" validation="ddlrequired">
                                                                    @foreach (var option in subField.DataSource)
                                                                    {
                                                                        <option value="@option.Value">@option.Text</option>
                                                                    }
                                                                </select>
                                                            }
                                                            else
                                                            {
                                                                <select name="@subField.Id" class="select">
                                                                    @foreach (var option in subField.DataSource)
                                                                    {
                                                                        <option value="@option.Value">@option.Text</option>
                                                                    }
                                                                </select>
                                                            }
                                                        }
                                                        </div>
                                                        @if (!string.IsNullOrEmpty(subField.ToolTip))
                                                        {
                                                            <span title="@subField.ToolTip" class="info" href="#"><em>info</em></span>
                                                        }
                                                    </div>
                                                }
                                                else if (subField.TypeId == 512)
                                                {
                                                    // Checkbox
                                                    <div class="row">
                                                        <label for="text1">@subField.Title</label>
                                                        <div class="groupinput">
                                                        @if (!string.IsNullOrEmpty(subField.Visibility) && subField.Visibility.ToUpper() == "READONLY")
                                                        {
                                                            <input type="checkbox" name="@subField.Id" disabled="disabled" />
                                                        }
                                                        else
                                                        {
                                                            <input type="checkbox" name="@subField.Id" />
                                                        }

                                                        </div>
                                                        @if (!string.IsNullOrEmpty(subField.ToolTip))
                                                        {
                                                            <span title="@subField.ToolTip" class="info" href="#"><em>info</em></span>
                                                        }
                                                    </div>
                                                }
                                                else if (subField.TypeId == 1024)
                                                {
                                                    // Radio button list
                                                    <div class="row">
                                                        <label for="text1">@subField.Title</label>
                                                        <div class="groupinput">
                                                            @if (subField.DataSource != null)
                                                            {
                                                                if (!string.IsNullOrEmpty(subField.Visibility) && subField.Visibility.ToUpper() == "READONLY")
                                                                {
                                                                    i = 0;
                                                                    foreach (var temp in subField.DataSource)
                                                                    {
                                                                        i++;
                                                                        inputTagName = "id" + i.ToString();

                                                                        if (temp.Selected)
                                                                        {
                                                                            <input id="@inputTagName" type="radio" name="@subField.Id" disabled="disabled" value="@temp.Value" checked="checked" /> @temp.Text
                                                                        }
                                                                        else
                                                                        {
                                                                            <input id="@inputTagName" type="radio" name="@subField.Id" disabled="disabled" value="@temp.Value" /> @temp.Text
                                                                        }
                                                                    }
                                                                }
                                                                else
                                                                {
                                                                    if (subField.Required)
                                                                    {
                                                                        i = 0;
                                                                        foreach (var temp in subField.DataSource)
                                                                        {
                                                                            i++;
                                                                            inputTagName = "id" + i.ToString();

                                                                            if (i == 1)
                                                                            {
                                                                                <input id="@inputTagName" type="radio" name="@subField.Id" validation="rdrequired" value="@temp.Value" /> @temp.Text
                                                                            }
                                                                            else
                                                                            {
                                                                                <input id="@inputTagName" type="radio" name="@subField.Id" value="@temp.Value" /> @temp.Text
                                                                            }
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        i = 0;
                                                                        foreach (var temp in subField.DataSource)
                                                                        {
                                                                            i++;
                                                                            inputTagName = "id" + i.ToString();
                                                                            <input id="@inputTagName" type="radio" name="@subField.Id" value="@temp.Value" /> @temp.Text
                                                                        }
                                                                    }
                                                                }

                                                            }
                                                        </div>
                                                        @if (!string.IsNullOrEmpty(subField.ToolTip))
                                                        {
                                                            <span title="@subField.ToolTip" class="info" href="#"><em>info</em></span>
                                                        }
                                                    </div>
                                                }
                                                else
                                                {
                                                    if (subField.Required)
                                                    {
                                                        Html.RenderPartial("_PartialGroupRequireFields", subField);
                                                    }
                                                    else
                                                    {
                                                        Html.RenderPartial("_PartialGroupFields", subField);
                                                    }
                                                }
                                            }
                                        </span>
                                    </li>
                                }
                            }
                        }
                        </ul>
                    }
                }
            </div>
        </div>

        <!-- Bottom function -->
        <div class="title nomargin end">
            <span class="left-btns">
                <a href="/Lead" class="btn btn-cancel-small">cancel</a>
            </span>
            <span class="right-btns">
                <input type="submit" id="submit" class="btn btn-submit-small" value="Submit" style="vertical-align: top;" onclick="return preValidateLeadForm(this);" />
            </span>
        </div>

        @Html.Partial("_PartialSubmitting")
        <div class="spacer25"></div>
    </div>

</fieldset>
</form>

<script type="text/javascript">
    var data = @(Html.Raw(Json.Encode(Model)));
</script>
<script src="/Scripts/lib/jquery.validate.custom.js" type="text/javascript"></script>
<script src="/Scripts/Leads/AddLeadViewModel.js" type="text/javascript"></script>